{extend name="layout" /}
{block name="content"}
<style>
	.x-nav{
		display:none;
	}
</style>
<div class="layui-card-body">
	<div data-model="table-bind">
		<table class="layui-hide" id="data_table" lay-filter="data_table" data-table></table>
	</div>
</div>
<script type="text/html" id="toolbarDemo">
	<div class="layui-btn-container">
	</div>
  </script>
<script type="text/html" id="barTar">

</script>
<script type="text/html" id="image">
	<img src="{{d.image}}" height="30">
</script>
<script type="text/html" id="sysTar">
	{{#  if(d.status == 1){ }}
  	<button type="button" class="layui-btn layui-btn-normal">正常</button>
  	{{#  } else { }}
  	<button type="button" class="layui-btn layui-btn-danger">关闭</button>
  	{{#  } }}
</script>
{/block}
{block name="js"}
<script type="text/javascript">
var mixin = {};
var SlefUrl = this.location.href;
layui.use(['table','upload'], function(){
	var table = layui.table
		,upload = layui.upload;
	table.render({
		elem: '#data_table',
		url:SlefUrl,
        toolbar: '#toolbarDemo',
        defaultToolbar: ['filter', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
            title: '导出Excel'
            ,layEvent: 'LAYTABLE_Excel'
            ,icon: 'layui-icon layui-icon-export'
        }],
		limit:20,
		cellMinWidth: 80,
		cols: [[
	      	// {field:'id',width:80, title: 'ID', sort: true,align:'center'}
	      	{field:'sn', title: '{$name}编号', width:180,align:'center'}
	      	,{field:'name', title: '{$name}名称'}
	      	,{field:'count_num', title: '分配数量'}
            ,{field:'res_num', title: '已核销数量',sort: true,align:'center'}
            ,{field:'res_mark', title: '剩余数量',sort: true,align:'center'}
	    ]],
	    page: true,
        id: 'testReload'
	});
	table.on('tool(data_table)', function(obj){
		var data = obj.data;
		data.table = "goods";
		if(obj.event === 'del'){
			layer.confirm('确定删除吗？', function(index){
	        	layer.close(index);
	       		form.formDel(data,obj);
	      	});
		}


	});

    //头工具栏事件
    table.on('toolbar(data_table)', function(obj){

        if(obj.event === 'LAYTABLE_Excel'){
            var result = {};
            result.customer_id = $('#customer_id').val();
            result.goods_id = $('#goods_id').val();
            result.staff_id = $('#staff_id').val();
            result.like = $('#like').val();
            result.start_time = $('#start_time').val();
            result.end_time = $('#end_time').val();

            DownLoadFile({'url':''+webRoot+ webControl+'/lists_excel?id={$id}&type={$type}','data':result})
        }

    });


    var $ = layui.$, active = {
        reload: function(){
            var title = $('#title');
            var gid = $('#gid');
            var spec = $('#spec');
            var customer = $('#customer');
            var cid = $('#cid');

            //执行重载
            table.reload('testReload', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                ,where: {
                    title: title.val()
                    ,gid : gid.val()
                    ,spec : spec.val()
                    ,customer: customer.val()
                    ,cid : cid.val()
                }
            }, 'data');
		},
		
		excelDemo: function(){
            window.location.href = '/excel/product_demo.xls'
        }


    };

	upload.render({
		elem: '#uploadExcel'
		,url: '{:url("savestudentImport")}'
		,accept: 'file' //普通文件
		,auto:'xlsx|xls|csv'
		,done: function(res){
			layer.alert(res.msg);
			// setInterval(function(){
			// 	window.location.reload();
			// },3000)
			
		}
	});

		// 
		

    $('.demoTable .layui-btn').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
	$('#excelDemo').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    var DownLoadFile = function (options) {
        var config = $.extend(true, { method: 'post' }, options);
        var $iframe = $('<iframe id="down-file-iframe" />');
        var $form = $('<form target="down-file-iframe" method="' + config.method + '" />');
        $form.attr('action', config.url);
        for (var key in config.data) {
            $form.append('<input type="hidden" name="' + key + '" value="' + config.data[key] + '" />');
        }
        $iframe.append($form);
        $(document.body).append($iframe);
        $form[0].submit();
        $iframe.remove();
    }


});
</script>
{/block}